package com.ruoyi.cfuser.rescue.service.impl;

import java.util.Collections;
import java.util.Date;
import java.util.List;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.redis.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.cfuser.rescue.mapper.RescueTaskMapper;
import com.ruoyi.cfuser.api.rescue.domain.RescueTask;
import com.ruoyi.cfuser.rescue.service.IRescueTaskService;

/**
 * 救援任务Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-02-20
 */
@Service
public class RescueTaskServiceImpl implements IRescueTaskService 
{
    @Autowired
    private RescueTaskMapper rescueTaskMapper;
    @Autowired
    private RedisService redisService;

    /**
     * 查询救援任务
     * 
     * @param id 救援任务主键
     * @return 救援任务
     */
    @Override
    public RescueTask selectRescueTaskById(Long id)
    {
        return rescueTaskMapper.selectRescueTaskById(id);
    }

    /**
     * 查询救援任务列表
     * 
     * @param rescueTask 救援任务
     * @return 救援任务
     */
    @Override
    public List<RescueTask> selectRescueTaskList(RescueTask rescueTask)
    {
        return rescueTaskMapper.selectRescueTaskList(rescueTask);
    }

//  根据用户id查询救援列表
    @Override
    public List<RescueTask> selectRescueTaskListByUserId(Long userId) {
        return rescueTaskMapper.selectRescueTaskListByUserId(userId);
    }

//  查询redis中向商户发送的数据
    @Override
    public List selectRedis(Long id){
        return redisService.getCacheList("taskList" + id);
    }
    /**
     * 新增救援任务
     * 
     * @param rescueTask 救援任务
     * @return 结果
     */
    @Override
    public int insertRescueTask(RescueTask rescueTask)
    {
        Snowflake snowflake = IdUtil.createSnowflake(1, 1);
        long id = snowflake.nextId();
        rescueTask.setId(id);
        Date date = new Date();
        String pattern = "yyyy-MM-dd HH:mm:ss";
        String format = DateUtil.format(date, pattern);
        DateTime parse = DateUtil.parse(format);
        rescueTask.setTime(parse);
        return rescueTaskMapper.insertRescueTask(rescueTask);
    }

    /**
     * 修改救援任务
     * 
     * @param rescueTask 救援任务
     * @return 结果
     */
    @Override
    public int updateRescueTask(RescueTask rescueTask)
    {
        return rescueTaskMapper.updateRescueTask(rescueTask);
    }

    /**
     * 批量删除救援任务
     * 
     * @param ids 需要删除的救援任务主键
     * @return 结果
     */
    @Override
    public int deleteRescueTaskByIds(Long[] ids)
    {
        return rescueTaskMapper.deleteRescueTaskByIds(ids);
    }

    /**
     * 删除救援任务信息
     * 
     * @param id 救援任务主键
     * @return 结果
     */
    @Override
    public int deleteRescueTaskById(Long id)
    {
        return rescueTaskMapper.deleteRescueTaskById(id);
    }
}
